package io.renren.modules.lucky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.validator.Assert;
import io.renren.modules.app.constants.ListConstants;
import io.renren.modules.app.form.out.AppCommentListForm;
import io.renren.modules.app.utils.StringUtils;
import io.renren.modules.lucky.dao.AppGoodsCommentDao;
import io.renren.modules.lucky.dao.AppUserScoreDao;
import io.renren.modules.lucky.entity.AppGoodsCommentEntity;
import io.renren.modules.lucky.entity.AppUserScoreEntity;
import io.renren.modules.lucky.service.AppGoodsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("appGoodsCommentService")
public class AppGoodsCommentServiceImpl extends ServiceImpl<AppGoodsCommentDao, AppGoodsCommentEntity> implements AppGoodsCommentService {

    @Autowired
    private AppGoodsCommentDao appGoodsCommentDao;

    @Autowired
    private AppUserScoreDao scoreDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AppGoodsCommentEntity> page = this.page(
                new Query<AppGoodsCommentEntity>().getPage(params), new QueryWrapper<AppGoodsCommentEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<AppGoodsCommentEntity> queryPageForApp(String curPage, String pageSize) {

        QueryWrapper<AppGoodsCommentEntity> commentWrapper = new QueryWrapper<AppGoodsCommentEntity>();
        commentWrapper.eq("comment_flag", 1);
        commentWrapper.orderByDesc("create_time");
        commentWrapper.last("limit " + curPage + "," + pageSize);

        List<AppGoodsCommentEntity> list = baseMapper.selectList(commentWrapper);
        Assert.isNull(list, "评论为空");
        return list;
    }

    @Transactional(readOnly = false)
    public Map<String, Object> addComment(AppGoodsCommentEntity commentEntity) {
        Map<String, Object> map = new HashMap<>();
        //如果评论信息为数字，则type为2
        boolean boo = StringUtils.isNumericZidai(commentEntity.getCommentContent());
        Date date = new Date();
        commentEntity.setCreateTime(date);
        commentEntity.setUpdateTime(date);
        if (boo)
            commentEntity.setCommentType("2");
        else
            commentEntity.setCommentType("1");
        commentEntity.setCommentFlag(1);
        int num = appGoodsCommentDao.insertComment(commentEntity);
        map.put("flag", num);
        if (num > 0) {
            Map<String, Object> commentMap = appGoodsCommentDao.getGoodsComment(commentEntity.getId());
            AppCommentListForm commentListForm = new AppCommentListForm();
            commentListForm.setCommentContent(commentMap.get("comment_content") != null ? commentMap.get("comment_content").toString() : "");
            commentListForm.setCommentId(commentMap.get("comment_id") != null ? Integer.parseInt(commentMap.get("comment_id").toString()) : 0);
            commentListForm.setUserPortrait(commentMap.get("user_portrait") != null ? commentMap.get("user_portrait").toString() : "");
            commentListForm.setUserNickname(commentMap.get("user_nickname") != null ? commentMap.get("user_nickname").toString() : "");
            commentListForm.setCommentCreateTime(commentMap.get("create_time").toString());
            map.put("comment", commentListForm);
            AppUserScoreEntity score = ListConstants.insertScore("comment", commentEntity.getUserId());
            scoreDao.insert(score);
        }
        return map;
    }

    public int addPrice(AppGoodsCommentEntity commentEntity) {
        commentEntity.setCommentType("2");
        return baseMapper.insert(commentEntity);
    }

}